﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Globalization;

namespace PropGridInfo
{
	public abstract class ComboBoxItemTypeConvert : TypeConverter
	{
		public ComboBoxItemTypeConvert()
		{
			this.myhash = new Hashtable();
			this.GetConvertHash();
		}

		public abstract void GetConvertHash();

		public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
		{
			return true;
		}

		public override TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
		{
			int[] array = new int[this.myhash.Values.Count];
			int num = 0;
			foreach (object obj in this.myhash)
			{
				DictionaryEntry dictionaryEntry = (DictionaryEntry)obj;
				array[num++] = (int)dictionaryEntry.Key;
			}
			return new TypeConverter.StandardValuesCollection(array);
		}

		public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
		{
			bool flag = sourceType == typeof(string);
			return flag || base.CanConvertFrom(context, sourceType);
		}

		public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object obj)
		{
			bool flag = obj is string;
			if (flag)
			{
				foreach (object obj2 in this.myhash)
				{
					DictionaryEntry dictionaryEntry = (DictionaryEntry)obj2;
					bool flag2 = dictionaryEntry.Value.Equals(obj.ToString());
					if (flag2)
					{
						this.m_strCurValue = obj.ToString();
						this.m_nCurIndex = (int)dictionaryEntry.Key;
						bool flag3 = !this.m_bConverse;
						if (flag3)
						{
							this.m_nCurIndex = this.myhash.Count - this.m_nCurIndex;
						}
						return dictionaryEntry.Key;
					}
				}
			}
			return base.ConvertFrom(context, culture, obj);
		}

		public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
		{
			bool flag = destinationType == typeof(string);
			return flag || base.CanConvertTo(context, destinationType);
		}

		public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object obj, Type destinationType)
		{
			bool flag = destinationType == typeof(string);
			object result;
			if (flag)
			{
				foreach (object obj2 in this.myhash)
				{
					DictionaryEntry dictionaryEntry = (DictionaryEntry)obj2;
					bool flag2 = dictionaryEntry.Key.Equals(obj);
					if (flag2)
					{
						return dictionaryEntry.Value.ToString();
					}
				}
				result = "";
			}
			else
			{
				result = base.ConvertTo(context, culture, obj, destinationType);
			}
			return result;
		}

		public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
		{
			return !this.m_bEditable;
		}

		public bool m_bEditable = false;

		public bool m_bConverse = false;

		public Hashtable myhash = null;

		public string m_strCurValue = "";

		public int m_nCurIndex = 0;
	}
}
